package com.wlyuan.index.remote.facade;

import com.wlyuan.core.domain.valueobj.TenantId;
import com.wlyuan.core.domain.valueobj.WaybillId;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.index.api.dto.waybill.WaybillIndexDTO;
import com.wlyuan.index.api.dto.waybill.WaybillReindexAllDTO;
import com.wlyuan.index.api.dto.waybill.WaybillReindexDTO;
import com.wlyuan.index.domain.source.adapter.WaybillAdapter;
import com.wlyuan.index.infrastructure.exception.WaybillNoFound;
import com.wlyuan.tools.Sort;
import com.wlyuan.transport.api.IWaybillQueryService;
import com.wlyuan.transport.dto.query.WaybillDataFilterDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuanjie
 * <p>
 * 运单防腐层
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WaybillFacade {
    @DubboReference(check = false)
    private IWaybillQueryService waybillQueryService;

    public WaybillIndexDTO getWaybillById(WaybillId waybillId) {
        return this.getWaybillById(waybillId.getId());
    }

    private WaybillIndexDTO getWaybillById(Long waybillId) {
        try {
            var waybill = waybillQueryService.getWaybillById(waybillId);
            AssertUtils.assertNotNull(waybill, new WaybillNoFound(), waybillId);
            waybill.setVersion(null);
            return WaybillAdapter.translate(waybill);
        } catch (Exception e) {
            logger.error("Waybill query failed: {}", waybillId, e);
        }
        return null;
    }

    public List<WaybillId> listIds(long page, long size) {
        WaybillReindexDTO reindex = new WaybillReindexDTO();
        return this.listIds(reindex, page, size);
    }

    public List<WaybillId> listIds(WaybillReindexAllDTO reindexAll, long page, long size) {
        WaybillReindexDTO reindex = new WaybillReindexDTO();
        BeanUtils.copyProperties(reindexAll, reindex);
        return this.listIds(reindex, page, size);
    }

    public List<WaybillId> listIds(WaybillReindexDTO reindex, long page, long size) {
        var filter = new WaybillDataFilterDTO();
        filter.setPage(page);
        filter.setSize(size);
        filter.setCreateTimeBegin(reindex.getCreateAtBegin());
        filter.setCreateTimeEnd(reindex.getCreateAtEnd());
        filter.setTenantId(reindex.getTenantId());
        var sorts = new ArrayList<Sort>();
        var sort = new Sort();
        sort.setAsc(false);
        sort.setName("id");
        sorts.add(sort);
        filter.setSort(sorts);
        return waybillQueryService.listIds(filter)
                .stream()
                .map(WaybillId::new)
                .collect(Collectors.toList());
    }

    public List<WaybillId> listIds(TenantId tenantId, long page, long size) {
        WaybillReindexDTO reindex = new WaybillReindexDTO();
        reindex.setTenantId(tenantId.getId());
        return this.listIds(reindex, page, size);
    }
}

